import requests
import json
from openpyxl import load_workbook
import datetime

# #hash:{live.broadcast}:<现场直播ID>
# 100051
# hgetall #hash:{live.broadcast}:100240

# 域名
luaUrl = 'https://app.aiyinsitanfm.com'
# 地址
geturl = '/getlivebroadcast'
dourl = '/dolivebroadcast'

# headers
headers = {
    'Content-Type': 'application/json'
}

# 错误码
success = "00001"
fail = "00000"

table_name = "live_broadcast"
# excel
wb2 = load_workbook('%s.xlsx' % (table_name))
ws=wb2.get_sheet_names()

# 写日志
def tolog( str ):
    print(str)
    print(str, file = f)

logtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
f = open('C:\\Users\\Administrator\\Desktop\\logs\\%s-%s.txt' % (table_name,logtime), "w")

tolog("start...")
for row in wb2:
    for cell in row:
        tolog('START============================================================================================START')
        ### 取lua数据
        getvalues = {"id":str(cell[0].value)}
        tolog('getvalues=======>>>' + json.dumps(getvalues) + '=======>>> #hash:{live.broadcast}:%s' % cell[0].value)
        getresp = requests.post(luaUrl + geturl, data = getvalues, headers = headers)
        tolog('getresp=======>>>' + getresp.content.decode('utf8'))

        # 获取错误码
        if json.loads(getresp.content)["status_code"] == success :
            tolog('缓存存在此KEY=======>>> #hash:{live.broadcast}:%s' % cell[0].value)
            ### 写lua数据
            # 是否存在key
            if "play_path" in getresp.content.decode('utf8'):
                dovalues = {"id":str(cell[0].value),"play_path":str(json.loads(getresp.content)["play_path"].replace("5506.liveplay.myqcloud.com", "liveplay.aiyinsitanfm.com"))}
                tolog('dovalues=======>>>' + json.dumps(dovalues))
                doresp = requests.post(luaUrl + dourl, data = dovalues, headers = headers)
                tolog('doresp=======>>>' + doresp.content.decode('utf8'))
            else:
                tolog('此KEY不存在play_path')

        else:
            tolog('缓存不存在此KEY=======>>> #hash:{live.broadcast}:%s' % cell[0].value)

        tolog('END============================================================================================END')

tolog("end...")
f.close()




